layui.define(['form', 'table'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form, element = layui.element;

  var typeId =$('input[name="id"]').val();

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/device/type/func/list.data'
        ,where:{typeId:typeId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'code',title: '功能Code',templet:function(record){
                return record.codeName + '('+record.code+')';
            }}
            ,{field: 'businessType', title: '业务Code',templet:function(record){
                if(record.businessType) {
                    return record.businessTypeName + '(' + record.businessType + ')';
                }else{
                    return '---';
                }
            }}
            ,{field: 'slotTotal',title: '服务端口数'}
            ,{field: 'protocolType',title: '通信协议'}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 120, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
        ]]
        ,page: false
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    table.render({
        elem: '#LAY-table-version'
        ,url: layui.setter.webRoot + '/device/type/version/list.data'
        ,where:{typeId:typeId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'versionNumber',title: '版本号'}
            ,{field: 'packagePath', title: '固件版本包'}
            ,{field: 'packageCRC',title: '固件版本CRC'}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 120, align:'center', fixed: 'right', toolbar: '#LAY-table-version-toolbar'}
        ]]
        ,page: false
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    var active = {
        add: function () {
            layer.open({
                type: 2
                , title: '新增功能'
                , content: layui.setter.webRoot + '/device/type/func/add.html?typeId='+typeId
                , maxmin: false
                , area: ['500px', '500px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-form-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/device/type/func/add.do',
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    layer.close(index);
                                    table.reload('LAY-table');
                                    layer.msg('新增成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,update: function (obj) {
            layer.open({
                type: 2
                , title: '修改功能'
                , content: layui.setter.webRoot + '/device/type/func/update.html?typeId='+obj.data.typeId + '&code='+obj.data.code
                , maxmin: false
                , area: ['500px', '500px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-form-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/device/type/func/update.do',
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    layer.close(index);
                                    table.reload('LAY-table');
                                    layer.msg('修改成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,setCost: function (obj) {
            parent.layer.open({
                type: 2
                , title: '配置价格'
                , content: layui.setter.webRoot + '/device/type/func/cost.html?typeId='+obj.data.typeId + '&code='+obj.data.code
                , maxmin: false
                , area: ['95%', '95%']
                , btn: ['取消']
            });
        }
        ,del:function(obj){
            layer.confirm('确定要删除该功能吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/device/type/func/del.do',
                    data:{typeId:obj.data.typeId,code:obj.data.code},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        ,addVersion: function () {
            layer.open({
                type: 2
                , title: '新增固件升级包'
                , content: layui.setter.webRoot + '/device/type/version/add.html?typeId='+typeId
                , maxmin: false
                , area: ['600px', '500px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-form-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/device/type/version/add.do',
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    layer.close(index);
                                    table.reload('LAY-table-version');
                                    layer.msg('新增成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,updateVersion: function (obj) {
            layer.open({
                type: 2
                , title: '修改固件升级包'
                , content: layui.setter.webRoot + '/device/type/version/update.html?typeId='+obj.data.typeId + '&versionNumber='+obj.data.versionNumber
                , maxmin: false
                , area: ['600px', '500px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-form-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/device/type/version/update.do',
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    layer.close(index);
                                    table.reload('LAY-table-version');
                                    layer.msg('修改成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,delVersion:function(obj){
            layer.confirm('确定要删除该固件升级包吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/device/type/version/del.do',
                    data:{typeId:obj.data.typeId,versionNumber:obj.data.versionNumber},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    }

    element.on('tab(tab-info)', function (data) {
        if (data.index == 0) {
            table.reload('LAY-table')
        }else if(data.index == 1) {
            table.reload('LAY-table-version')
        }
    });

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

    table.on('tool(LAY-table-version)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });
  
  exports('device_type_info', {})
});